import {
  Card,
  Breadcrumb,
  Form,
  Button,
  Radio,
  DatePicker,
  Table,
  Tag,
  Space,
  Image,
  message,
  Modal,
} from 'antd'
import {
  EditOutlined,
  DeleteOutlined,
  ExclamationCircleOutlined,
} from '@ant-design/icons'
import React, { Component } from 'react'
import { Link } from 'react-router-dom'
import { constant } from 'api/constant'
import Channel from 'components/Channel'
import { getArticlesList, delArticles } from 'api/article'
import defaultImg from 'assets/error.png'

const { RangePicker } = DatePicker
export default class articleList extends Component {
  columns = [
    {
      title: '封面',
      dataIndex: 'cover',
      render(data) {
        if (data.type === 0) {
          return (
            <Image width={200} preview={false} height={150} src={defaultImg} />
          )
        }
        return (
          <Image
            style={{ objectFit: 'cover' }}
            width={200}
            preview={false}
            height={150}
            src={data.images[0]}
            fallback={defaultImg}
          />
        )
      },
    },
    {
      title: '标题',
      dataIndex: 'title',
    },
    {
      title: '状态',
      dataIndex: 'status',
      render(data) {
        // console.log(data)
        const obj = constant.find((item) => item.id === data)
        // console.log(obj)
        return <Tag color={obj.color}>{obj.name}</Tag>
      },
    },
    {
      title: '发布时间',
      dataIndex: 'pubdate',
    },
    {
      title: '阅读数',
      dataIndex: 'read_count',
    },
    {
      title: '评论数',
      dataIndex: 'comment_count',
    },
    {
      title: '点赞数',
      dataIndex: 'like_count',
    },
    {
      title: '操作',
      render: (data) => {
        return (
          <Space>
            <Button
              type="primary"
              shape="circle"
              icon={<EditOutlined />}
              onClick={() => this.handleEdit(data.id)}
            />
            <Button
              onClick={() => this.hanleClick(data.id)}
              danger
              type="primary"
              shape="circle"
              icon={<DeleteOutlined />}
            />
          </Space>
        )
      },
    },
  ]
  params = {
    page: 1,
    per_page: 10,
  }
  state = {
    articles: {},
    total: 0,
  }
  render() {
    return (
      <div className="article">
        <Card
          title={
            <Breadcrumb>
              <Breadcrumb.Item>
                <Link to="/home">首页</Link>
              </Breadcrumb.Item>
              <Breadcrumb.Item>文章列表</Breadcrumb.Item>
            </Breadcrumb>
          }
        >
          <Form onFinish={this.onFinish} initialValues={{ status: -1 }}>
            <Form.Item label="状态" name="status">
              <Radio.Group>
                {constant.map((item) => (
                  <Radio key={item.id} value={item.id}>
                    {item.name}
                  </Radio>
                ))}
              </Radio.Group>
            </Form.Item>
            <Form.Item label="频道" name="channels_id">
              <Channel></Channel>
            </Form.Item>
            <Form.Item label="日期" name="date">
              <RangePicker></RangePicker>
            </Form.Item>
            <Form.Item>
              <Button type="primary" htmlType="submit">
                筛选
              </Button>
            </Form.Item>
          </Form>
        </Card>
        <Card title={`根据筛选条件共查询到${this.state.total}条数据`}>
          <Table
            rowKey="id"
            dataSource={this.state.articles.results}
            columns={this.columns}
            pagination={{
              position: ['bottomCenter'],
              total: this.state.total,
              pageSize: this.state.articles.per_page,
              current: this.state.articles.page,
              onChange: this.handleChange,
            }}
          />
        </Card>
      </div>
    )
  }
  //修改功能
  handleEdit = (id) => {
    // console.log(id)
    this.props.history.push(`/home/publish/${id}`)
  }
  //删除
  hanleClick = (id) => {
    // console.log(id)
    Modal.confirm({
      title: '提示',
      icon: <ExclamationCircleOutlined />,
      content: '确认删除吗',
      onOk: async () => {
        await delArticles(id)
        message.success('删除成功')
        this.getArticles()
      },
    })
  }
  //筛选
  onFinish = async ({ status, channels_id, date }) => {
    console.log(status, channels_id, date)
    if (status !== -1) {
      this.params.status = status
    } else {
      delete this.params.status
    }
    if (channels_id !== undefined) {
      this.params.channels_id = channels_id
    } else {
      delete this.params.channels_id
    }
    if (date) {
      this.params.begin_pubdate = date[0]
        .startOf('day')
        .format('YYYY-MM-DD HH-mm-ss')
      this.params.end_pubdate = date[1]
        .endOf('day')
        .format('YYYY-MM-DD HH-mm-ss')
    } else {
      delete this.params.begin_pubdate
      delete this.params.end_pubdate
    }
    this.getArticles()
  }
  async componentDidMount() {
    this.getArticles()
  }
  //获取文章数据
  getArticles = async () => {
    const res = await getArticlesList(this.params)
    // console.log(res)
    this.setState({
      articles: res.data,
      total: res.data.total_count,
    })
  }
  //切换页码
  handleChange = async (page, per_page) => {
    this.params.page = page
    this.params.per_page = per_page
    this.getArticles()
  }
}
